Skip to content

Fix: Sanitize parameter descriptions to prevent code generation errors#1

Merged
coderloom merged 1 commit into
mainfrom
fix/sanitize-parameter-descriptions
Oct 25, 2025
Merged

Fix: Sanitize parameter descriptions to prevent code generation errors#1
coderloom merged 1 commit into
mainfrom
fix/sanitize-parameter-descriptions

Conversation

@kamalsrini
Copy link
Copy Markdown

Problem

Parameter descriptions from OpenAPI specifications were not being sanitized, causing TypeScript compilation errors in generated MCP servers when descriptions contained:

  • Newlines (line breaks)
  • Single quotes
  • HTML entities (<, >, `, ', etc.)

This resulted in unterminated string literals in the generated code, breaking the build process for servers created from well-documented APIs like Azure OpenAI.

Example Error

src/index.ts(369,156): error TS1002: Unterminated string literal.

Solution

  • Added sanitizeDescription() private method to centralize description sanitization logic
  • Method handles:
    • HTML entity decoding (<, >, ", ', `, &)
    • Newline removal and whitespace collapsing
    • Single quote escaping for JavaScript string safety
  • Updated all 8 locations where parameter descriptions are assigned to use the new sanitization method

Changes

  • src/generator/ApiParser.ts: Added sanitizeDescription() method and updated all description assignments

Impact

  • ✅ Fixes build failures for generated servers with complex API documentation
  • ✅ Ensures consistent description handling across all parsing methods (OpenAPI, Postman, plain text)
  • ✅ Prevents future issues with special characters in descriptions

Testing

  • Existing dev server continues to run without issues
  • TypeScript compilation passes
  • Changes are backwards compatible

Related Issue

Fixes build failures when generating MCP servers from APIs with multi-line parameter descriptions (e.g., Azure OpenAI API).

## Problem
Parameter descriptions from OpenAPI specifications were not being sanitized,
causing TypeScript compilation errors in generated MCP servers when descriptions
contained:
- Newlines (line breaks)
- Single quotes
- HTML entities (<, >, `, ', etc.)

This resulted in unterminated string literals in the generated code, breaking
the build process for servers created from well-documented APIs like Azure
OpenAI.

## Solution
- Added `sanitizeDescription()` private method to centralize description
  sanitization logic
- Method handles:
  - HTML entity decoding (<, >, ", ', `, &)
  - Newline removal and whitespace collapsing
  - Single quote escaping for JavaScript string safety
- Updated all 8 locations where parameter descriptions are assigned to use
  the new sanitization method

## Impact
- Fixes build failures for generated servers with complex API documentation
- Ensures consistent description handling across all parsing methods
  (OpenAPI, Postman, plain text)
- Prevents future issues with special characters in descriptions

Fixes issue with generated server build failures due to unterminated string
literals in parameter descriptions.
@coderloom coderloom merged commit e9b2860 into main Oct 25, 2025
2 checks passed
@coderloom coderloom deleted the fix/sanitize-parameter-descriptions branch October 25, 2025 05:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants